package it.polimi.authentication.web.beans;

import java.util.ArrayList;
import java.util.List;

import it.polimi.authentication.ejb.usermanagement.AddNewHotel;
import it.polimi.authentication.ejb.usermanagement.ListaHotel;
import it.polimi.authentication.ejb.usermanagement.dto.HotelDTO;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.model.DualListModel;




@ManagedBean (name="RetrieveHotel")
@SessionScoped
public class RetrieveHotel {
	
	@EJB
	private ListaHotel LH;
	
	@EJB
	private AddNewHotel DH;
	
	private ArrayList<HotelDTO> HotelL;
	private ArrayList<String> HotelLSel;
	private DualListModel<String> HotelDL; 
	
	
	
	public ArrayList<String> getHotelLSel() {
		return HotelLSel;
	}

	public void setHotelLSel(ArrayList<String> hotelLSel) {
		HotelLSel = hotelLSel;
	}


	public DualListModel<String> getHotelDL() {
		return HotelDL;
	}

	public void setHotelDL(DualListModel<String> hotelDL) {
		HotelDL = hotelDL;
	}

	public RetrieveHotel()
	{
	}

	public ArrayList<HotelDTO> getHotelL() {
		return HotelL;
	}
	
	public void setHotelL(ArrayList<HotelDTO>  HotelL) {
		this.HotelL = HotelL;
	}


	
	public void myHotel()
	{
		
		HotelL = LH.AllHotel(); 
		ArrayList<String> source = new ArrayList<String>();  
        ArrayList<String> target = new ArrayList<String>();  
		for (int i=0; i<HotelL.size(); i++)
		{
			System.out.println (HotelL.get(i).getNome());
			source.add(HotelL.get(i).getNome());
			
		}
		HotelDL = new DualListModel<String>(source, target);
		//System.out.println("la mappa"+citta);
		
	}
	
	private void aggiorna()
	{

		myHotel();
		
	}
	
	public void CancellaH()
	{
		if(!HotelLSel.isEmpty()){
			DH.delete(HotelLSel);
		
			FacesContext context = FacesContext.getCurrentInstance();  
			context.addMessage(null, new FacesMessage("Hotel cancellato/i con successo!", "" )); 
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("RetrieveHotel", null);		

			RequestContext context2 = RequestContext.getCurrentInstance();
			context2.update("formH");
		}
		else
		{
			FacesContext context = FacesContext.getCurrentInstance(); 
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR ,"Attenzione!", "Seleziona almeno un hotel da cancellare!"));
		}
		
	}
	
}
